/* custom scrollbar plugin */
(function($){
	$(window).on("load",function(){
		if($(".FrameRight .TreeWrap")){
			$(".FrameRight .TreeWrap").css('max-height',($(window).height()-60)+'px');
		}
		$.mCustomScrollbar.defaults.scrollButtons.enable=true; //enable scrolling buttons by default
		$.mCustomScrollbar.defaults.axis="yx"; //enable 2 axis scrollbars by default
		$(".TreeWrap").mCustomScrollbar({
			scrollInertia: 10,
			axis: "xy",
			theme:"minimal-dark",
			scrollbarPosition: "inside"
		});
	});
})(jQuery);
	
//初始化
$(document).ready(function(){	
	$(function(){
		// window页，win-menu固定
		var a = $("#winMenuSetting");
		$(window).scroll(function() {
			var b = $(window).scrollTop();
			b > 0 && a.addClass("win-menu-style2") && a.removeClass("win-menu-style1"), 80 >= b && a.removeClass("win-menu-style2") && a.addClass("win-menu-style1 ")
		})
		/*重写select*/
		SelectModel();
	})
	
	/* 监听输入字数不能超过n */
	var num = '';
	$('textarea[limit-toggle="limitInput"]').bind('input propertychange', function() {
		num = $(this).attr('limit-num');
		if(num=='' || num==null || num==undefined){
			num = 200;
		}
		if($(this).val().length > num){
			$(this).val($(this).val().slice(0,num));
		}
		$(this).parent().find('span>em').text($(this).val().length);
	});
	
	/* 日期选择器 */
	$('.form_date').datetimepicker({
	    language:  'zh-CN',  
		autoclose: 1,
		minView: 2
	});
	
	/* 日期选择器 */
	$('.form_month').datetimepicker({
		weekStart: 1,  
        autoclose: true,  
        startView: 3,  
        minView: 3,  
        forceParse: false,  
        language: 'zh-CN',
		format: 'yyyy-mm'
	});
}); //结束

function WinPrint(tarUrl){	
    window.open (tarUrl,'','height=650,width=1200,top=10,left=80,toolbar=no,menubar=no,scrollbars=yes,resizable=no, location=no,status=no') ;
	return false;
}
function WinPrint2(tarUrl){
    window.open (tarUrl,'','') ;
	return false;
}
function WinClose(){
	  window.opener=null;    
	  window.open("","_self");    
	  window.close();    
}
function WinTip(tarUrl){	
    window.open (tarUrl,'','height=310,width=585,top=10,left=80,toolbar=no,menubar=no,scrollbars=yes,resizable=no, location=no,status=no') ;
	return false;
}
function WinTipopen(tarUrl){
	window.open (tarUrl,'','height=400,width=504,top=100,left=350,toolbar=no,menubar=no,scrollbars=yes,resizable=no, location=no,status=no') ;
	return false;
}
function SelectCardChange(str,type,val){
	$("."+str+" .selectcard a").removeClass("curr");
	$("."+str+" .selectcard #slt_a_"+val).addClass("curr");
	$("."+str+" .selectbox").hide();
	$("."+str+" #slt_box_"+val).show();	
}

/*重写select*/
function SelectModel(){
	var selectOption = $('[data-toggle="selectOption"]');
	var options = '';
	$('[data-toggle="select"]').bind('click',function(event){
		event = event||window.event;  
	    event.stopPropagation();
	    selectOption.hide(260);
		options = $(this).parent().find('[data-toggle="selectOption"]');
		if(options.css("display")=='block'){ options.hide(260); }
		else{ options.show(260); }
	});
	selectOption.find('li').bind('click',function(){
		$(this).parent().parent().find('[data-toggle="select"]>.fl').text($(this).text());
	})
	//TODO 失去焦点时要做的事情
	$(document).click(function(e){
		$('[data-toggle="selectOption"]').hide(260);  
	});
}
/*获取url参数*/
function GetUrlParms(){
    var args=new Object();   
    var query=location.search.substring(1);//获取查询串   
    var pairs=query.split("&");//在逗号处断开   
    for(var   i=0;i<pairs.length;i++) {   
        var pos=pairs[i].indexOf('=');//查找name=value   
        if(pos==-1)   continue;//如果没有找到就跳过   
        var argname=pairs[i].substring(0,pos);//提取name   
        var value=pairs[i].substring(pos+1);//提取value   
        args[argname]=unescape(value);//存为属性   
    }
    return args;
}

/* iframe自适应高度 */
function changeFrameHeight(){
	var contTableOffset = $('.cont-table').offset().top;
	var winHeight = $(window).height();
	$('.iframeWrap').find('iframe').height(winHeight-contTableOffset);
};
/*window.onresize=function(){  
    changeFrameHeight();  
};*/

/**
 * 打开modal：showModal(modalId,rtue)/showModal(modalId)
 * 打开父页面的modal：parent.showModal(modalId,rtue)
 */
function showModal(modalId,backdrop) {
	if(backdrop){
		$('#'+modalId).modal({
			backdrop: 'static', 
			keyboard: false
		});
	}else{
		$('#'+modalId).modal('show');
	}
};
function showParModal(modalId,backdrop){
	parent.showModal(modalId,backdrop);
};

/**
 * 关闭modal: hideModal(modalId)
 * 关闭父页面的modal：parent.hideModal(modalId)
 */
function hideModal(modalId){
	$('#'+modalId).modal('hide');
};
/**
 * 限制input 只能输入数字
 * eg: <input data-number />
 */
$('input[data-number]').bind('input propertychange', function() {
	var reg = /[^\d]/ig;
	$(this).val($(this).val().replace(reg,''));
});


/**
 * 日期加上天数得到新的日期  
 * dateTemp 需要参加计算的日期，days要添加的天数，返回新的日期，日期格式：YYYY-MM-DD  
 */
function getNewDay(calDate, days) {
	//将传进来日期转换成yyyy-mm-dd的字符串
	var mon = calDate.getMonth() + 1;
	var day = calDate.getDate();
	var dateTemp = calDate.getFullYear() + "-" + (mon<10?"0"+mon:mon) + "-" +(day<10?"0"+day:day)
	//切割字符串分别得年月日字符串数组
	var dateTemp = dateTemp.split("-");  
	var nDate = new Date(dateTemp[1] + '/' + dateTemp[2] + '/' + dateTemp[0]); //转换为MM-DD-YYYY格式    
	var millSeconds = Math.abs(nDate) + (days * 24 * 60 * 60 * 1000);  
	var rDate = new Date(millSeconds);  
	var year = rDate.getFullYear();  
	var month = rDate.getMonth() + 1;  
	if (month < 10) month = "0" + month;  
	var date = rDate.getDate();  
	if (date < 10) date = "0" + date;  
	return (year + "-" + month + "-" + date);  
}  

/**
 * loading  
 */
var loader={
	field: {
        loading: $('<div id="loader" class="loader"><div class="loading"><div class="dot"></div><div class="dot"></div><div class="dot"></div><div class="dot"></div><div class="dot"></div></div></div>')
    },
    open: function (page) {
        var _this = loader;
        _this.field.loading.show();
        $(page).prepend(_this.field.loading);
    },
    close: function (page) {
        var _this = loader;
        $(page).find("#loader").remove();
    }	
};



